package com.jingdianjichi.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;

import com.jingdianjichi.api.subject.entity.dto.PracticeSubjectDTO;
import com.jingdianjichi.api.subject.entity.po.SubjectPO;
import com.jingdianjichi.api.subject.entity.vo.PracticeSubjectOptionVO;
import com.jingdianjichi.api.subject.entity.vo.PracticeSubjectVO;
import com.jingdianjichi.subject.common.entity.PageResult;
import com.jingdianjichi.subject.common.enums.IsDeletedFlagEnum;
import com.jingdianjichi.subject.common.enums.SubjectInfoTypeEnum;
import com.jingdianjichi.subject.common.redis.RedisUtil;
import com.jingdianjichi.subject.common.util.IdWorkerUtil;
import com.jingdianjichi.subject.common.util.LoginUtil;
import com.jingdianjichi.subject.domain.comvert.SubjectInfoConverter;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.entity.SubjectOptionBO;
import com.jingdianjichi.subject.domain.handler.subject.SubjectTypeHandler;
import com.jingdianjichi.subject.domain.handler.subject.SubjectTypeHandlerFactory;
import com.jingdianjichi.subject.domain.service.SubjectDomainService;
import com.jingdianjichi.subject.domain.service.SubjectLikedDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectInfo;
import com.jingdianjichi.subject.infra.basic.entity.SubjectInfoEs;
import com.jingdianjichi.subject.infra.basic.entity.SubjectLabel;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMapping;
import com.jingdianjichi.subject.infra.basic.service.SubjectEsService;
import com.jingdianjichi.subject.infra.basic.service.SubjectInfoService;
import com.jingdianjichi.subject.infra.basic.service.SubjectLabelService;
import com.jingdianjichi.subject.infra.basic.service.SubjectMappingService;
import com.jingdianjichi.subject.infra.rpc.UserInfo;
import com.jingdianjichi.subject.infra.rpc.UserRpc;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SubjectDomainServiceImpl implements SubjectDomainService {

    @Resource
    private SubjectInfoService subjectInfoService;
    @Resource
    private SubjectTypeHandlerFactory subjectTypeHandlerFactory;
    @Resource
    private SubjectMappingService subjectMappingService;

    @Resource
    private SubjectLabelService subjectLabelService;
    @Resource
    private SubjectEsService subjectEsService;
    @Resource
    private SubjectLikedDomainService subjectLikedDomainService;
    @Resource
    private UserRpc userRpc;

    @Resource
    private RedisUtil redisUtil;

    private static final String RANK_KEY = "subject_rank";

    /**
     * 新增题目
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SubjectInfoBO subjectInfoBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectDomainServiceImpl.add.bo:{}", JSON.toJSONString(subjectInfoBO));

        }

        SubjectInfo subjectInfo = SubjectInfoConverter.INSTANCE.convertBOToInfo(subjectInfoBO);
        subjectInfo.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        subjectInfoService.insert(subjectInfo);

        SubjectTypeHandler handler = subjectTypeHandlerFactory.getHandler(subjectInfo.getSubjectType());
        subjectInfoBO.setId(subjectInfo.getId());
        handler.add(subjectInfoBO);

        LinkedList<SubjectMapping> mappingList = new LinkedList<>();
        List<Long> categoryIds = subjectInfoBO.getCategoryIds();
        List<Long> labelIds = subjectInfoBO.getLabelIds();
        for (Long categoryId : categoryIds) {
            for (Long labelId : labelIds) {
                SubjectMapping subjectMapping = new SubjectMapping();
                subjectMapping.setCategoryId(categoryId);
                subjectMapping.setLabelId(labelId);
                subjectMapping.setSubjectId(subjectInfo.getId());
                subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
                mappingList.add(subjectMapping);
            }
        }
        subjectMappingService.batchInsert(mappingList);

        //同步到es
        SubjectInfoEs subjectInfoEs = new SubjectInfoEs();
        subjectInfoEs.setDocId(new IdWorkerUtil(1, 1, 1).nextId());
        subjectInfoEs.setSubjectId(subjectInfo.getId());
        subjectInfoEs.setSubjectAnswer(subjectInfoBO.getSubjectAnswer());
        subjectInfoEs.setCreateTime(new Date().getTime());
        subjectInfoEs.setCreateUser(LoginUtil.getLoginId());
        subjectInfoEs.setSubjectName(subjectInfo.getSubjectName());
        subjectInfoEs.setSubjectType(subjectInfo.getSubjectType());
        subjectEsService.insert(subjectInfoEs);

        //放到redis记录排行榜
        redisUtil.addScore(RANK_KEY, LoginUtil.getLoginId(), 1);
    }

    /**
     * 查询题目列表
     *
     * @return
     */
    @Override
    public PageResult<SubjectInfoBO> getSubjectPage(SubjectInfoBO subjectInfoBO) {
        PageResult<SubjectInfoBO> pageResult = new PageResult<>();
        pageResult.setPageNo(subjectInfoBO.getPageNo());
        pageResult.setPageSize(subjectInfoBO.getPageSize());
        int start = (subjectInfoBO.getPageNo() - 1) * subjectInfoBO.getPageSize();
        SubjectInfo subjectInfo = SubjectInfoConverter.INSTANCE.convertBOToInfo(subjectInfoBO);

        int count = subjectInfoService.countByCondition(subjectInfo, subjectInfoBO.getCategoryId(), subjectInfoBO.getLabelId());
        List<SubjectInfo> subjectInfoList = subjectInfoService.queryPage(subjectInfo, subjectInfoBO.getCategoryId(),
                subjectInfoBO.getLabelId(), start, subjectInfoBO.getPageSize());
        List<SubjectInfoBO> infoBOList = SubjectInfoConverter.INSTANCE.convertListInfoToBO(subjectInfoList);
        pageResult.setRecords(infoBOList);
        pageResult.setTotal(count);
        return pageResult;
    }

    /**
     * 查询题目信息
     *
     * @return
     */
    @Override
    public SubjectInfoBO getSubjectInfo(SubjectInfoBO subjectInfoBO) {
        SubjectInfo subjectInfo = subjectInfoService.queryById(subjectInfoBO.getId());

        SubjectTypeHandler handler = subjectTypeHandlerFactory.getHandler(subjectInfo.getSubjectType());
        SubjectOptionBO subjectOptionBO = handler.query(subjectInfo.getId());
        SubjectInfoBO infoBO = SubjectInfoConverter.INSTANCE.convertOptionAndInfoToBO(subjectOptionBO, subjectInfo);

        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setSubjectId(subjectInfo.getId());
        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        List<SubjectMapping> mappingList = subjectMappingService.queryLabelId(subjectMapping);
        List<Long> labelIdList = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
        List<SubjectLabel> labelList = subjectLabelService.batchQueryById(labelIdList);
        List<String> labelNameList = labelList.stream().map(SubjectLabel::getLabelName).collect(Collectors.toList());
        infoBO.setLabelName(labelNameList);

        infoBO.setLiked(subjectLikedDomainService.isLiked(subjectInfo.getId().toString(), LoginUtil.getLoginId()));
        infoBO.setLikedCount(subjectLikedDomainService.getLikedCount(subjectInfo.getId().toString()));

        //快速刷题切换
        assembleSubjectCursor(subjectInfoBO, infoBO);
        return infoBO;
    }

    private void assembleSubjectCursor(SubjectInfoBO subjectInfoBO, SubjectInfoBO infoBO) {
        Long categoryId = subjectInfoBO.getCategoryId();
        Long labelId = subjectInfoBO.getLabelId();
        Long subjectId = subjectInfoBO.getId();
        if (Objects.isNull(categoryId) || Objects.isNull(labelId)) {
            return;
        }
        Long nextSubjectId = subjectInfoService.querySubjectIdCursor(subjectId, categoryId, labelId, 1);
        infoBO.setNextSubjectId(nextSubjectId);
        Long lastSubjectId = subjectInfoService.querySubjectIdCursor(subjectId, categoryId, labelId, 0);
        infoBO.setLastSubjectId(lastSubjectId);
    }

    /**
     * 全文检索
     */
    @Override
    public PageResult<SubjectInfoEs> getSubjectPageBySearch(SubjectInfoBO subjectInfoBO) {
        SubjectInfoEs subjectInfoEs = new SubjectInfoEs();
        subjectInfoEs.setPageNo(subjectInfoBO.getPageNo());
        subjectInfoEs.setPageSize(subjectInfoBO.getPageSize());
        subjectInfoEs.setKeyWord(subjectInfoBO.getKeyWord());
        return subjectEsService.querySubjectList(subjectInfoEs);
    }

    /**
     * 获取题目贡献榜
     */
    @Override
    public List<SubjectInfoBO> getContributeList() {
        Set<ZSetOperations.TypedTuple<String>> typedTuples = redisUtil.rankWithScore(RANK_KEY, 0, 5);
        if (CollectionUtils.isEmpty(typedTuples)) {
            return Collections.emptyList();
        }
        List<SubjectInfoBO> subjectInfoBOList = new LinkedList<>();
        for (ZSetOperations.TypedTuple<String> typedTuple : typedTuples) {
            SubjectInfoBO result = new SubjectInfoBO();
            result.setSubjectCount(typedTuple.getScore().intValue());
            UserInfo userInfo = userRpc.getUserInfo(typedTuple.getValue());

            result.setCreateUser(userInfo.getNickName());
            result.setCreateUserAvatar(userInfo.getAvatar());
            subjectInfoBOList.add(result);
        }
       /* List<SubjectInfo> subjectInfoList = subjectInfoService.getContributeList();

        if (CollectionUtils.isEmpty(subjectInfoList)) {
            return Collections.emptyList();
        }
        List<SubjectInfoBO> subjectInfoBOList = new LinkedList<>();
        for (SubjectInfo subjectInfo : subjectInfoList) {
            SubjectInfoBO result = new SubjectInfoBO();
            result.setSubjectCount(subjectInfo.getSubjectCount());
            UserInfo userInfo = userRpc.getUserInfo(subjectInfo.getCreatedBy());

            result.setCreateUser(userInfo.getNickName());
            result.setCreateUserAvatar(userInfo.getAvatar());
            subjectInfoBOList.add(result);
        }*/
        return subjectInfoBOList;
    }

    @Override
    public PracticeSubjectVO getPracticeSubject(PracticeSubjectDTO practiceSubjectDTO) {

        PracticeSubjectVO practiceSubjectVO = new PracticeSubjectVO();
        SubjectInfo subjectInfo = subjectInfoService.queryById(practiceSubjectDTO.getSubjectId());
        practiceSubjectVO.setSubjectName(subjectInfo.getSubjectName());
        practiceSubjectVO.setSubjectType(subjectInfo.getSubjectType());


        SubjectTypeHandler handler = subjectTypeHandlerFactory.getHandler(practiceSubjectDTO.getSubjectType());
        SubjectOptionBO subjectOptionBO = handler.query(practiceSubjectDTO.getSubjectId());


        List<PracticeSubjectOptionVO> optionList = new LinkedList<>();
        subjectOptionBO.getOptionList().forEach(e -> {
            PracticeSubjectOptionVO practiceSubjectOptionVO = new PracticeSubjectOptionVO();
            practiceSubjectOptionVO.setOptionContent(e.getOptionContent());
            practiceSubjectOptionVO.setOptionType(e.getOptionType());
            optionList.add(practiceSubjectOptionVO);
        });
        practiceSubjectVO.setOptionList(optionList);
        return practiceSubjectVO;
    }
}
